<script setup>

</script>

<template>
  <el-card>
    <!-- 查询条件开始 -->
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="字典名称" prop="dictName">
        <el-input
            v-model="queryParams.data.dictName"
            placeholder="请输入字典名称"
            clearable
            size="small"
            style="width:240px"
        />
      </el-form-item>
      <el-form-item label="字典类型" prop="dictType">
        <el-input
            v-model="queryParams.data.dictType"
            placeholder="请输入字典类型"
            clearable
            size="small"
            style="width:240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
            v-model="queryParams.data.status"
            placeholder="字典状态"
            clearable
            size="small"
            style="width:240px"
        >
          <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
            v-model="queryParams.data.dateRange"
            size="small"
            style="width:240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholde="开始日期"
            end-placeholde="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 查询条件结束 -->

    <!-- 表格工具按钮开始 -->
    <el-row :gutter="10" style="margin-bottom: 8px;">
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">
          批量删除
        </el-button>
      </el-col>
    </el-row>
    <!-- 表格工具按钮结束 -->

    <!-- 数据表格开始 -->
    <el-table border :data="dictTypeTableList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="字典编号" prop="dictId" align="center"/>
      <el-table-column label="字典名称" prop="dictName" align="center" :show-overflow-tooltip="true"/>
      <el-table-column label="字典类型" prop="dictType" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <router-link :to="'/system/dictData/' + scope.row.dictId" class="link-type">
            <span>{{ scope.row.dictType }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="statusName" align="center"/>
      <el-table-column label="备注" prop="remark" align="center" :show-overflow-tooltip="true"/>
      <el-table-column label="创建时间" prop="createTime" align="center" width="180"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" size="mini" @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-button type="text" icon="el-icon-delete" size="mini" @click="handleDelete(scope.row)" :disabled="single">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 数据表格结束 -->
    <!-- 分页控件开始 -->
    <el-pagination
        v-show="total>0"
        :current-page="queryParams.pageNo"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
    <!-- 分页控件结束 -->

    <!-- 添加修改弹出层开始 -->
    <el-dialog
        :title="title"
        :visible.sync="open"
        width="500px"
        center
        append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="字典名称" prop="dictName">
          <el-input v-model="form.dictName" placeholder="请输入字典名称" clearable size="small"/>
        </el-form-item>
        <el-form-item label="字典类型" prop="dictType">
          <el-input v-model="form.dictType" placeholder="请输入字典类型" clearable size="small"/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
                :value="dict.dictValue"
            >{{ dict.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入字典备注" clearable
                    size="small"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
    </el-dialog>
    <!-- 添加修改弹出层结束 -->
  </el-card>
</template>
<script>
export default {
  components: {},
  props: {},
  data() {
    return {
      //查询表单
      queryParams: {
        data:{},
        pageNo: 1,
        pageSize: 5,
        /*dictName: undefined,
        dictType: undefined,
        status: undefined*/
      },
      // 状态数据字典
      statusOptions: [],
      // 选中数组
      dictIds: [],
      // 非单个禁用
      single: false,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 字典表格数据
      dictTypeTableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //添加修改的数据
      form: {},
      //表单校验
      rules: {
        dictName: [
          {required: true, message: "字典名称不能为空", trigger: "blur"}
        ],
        dictType: [
          {required: true, message: "字典类型不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "状态不能为空", trigger: "blur"}
        ],
        remark: [
          {required: true, message: "备注不能为空", trigger: "blur"}
        ]
      },
    }
  },
  created() {
    this.formdata()
    this.loadStatus()
  },
  methods: {
    //表格数据
    formdata() {
      this.$http.post('/dictType/queryByPage', this.queryParams).then(res => {
        this.dictTypeTableList = res.data.data.list
        this.total = res.data.data.total
      })
    },
    //获取状态字典
    loadStatus() {
      this.$http.get('/dictType/getStatus').then(res => {
        this.statusOptions = res.data.data
      })
    },
    //搜索
    handleQuery() {
      this.formdata()
    },
    //重置搜索
    resetQuery() {
      this.queryParams = {
        pageNo: 1,
        pageSize: 5,
        data:{},
        /*dictName: undefined,
        dictType: undefined,
        status: undefined*/
      }
      this.formdata()
      // location.reload()//页面刷新
    },
    //添加
    handleAdd() {
      this.title = "添加数据"
      this.open = true
      this.form = {
        dictName: undefined,
        dictType: undefined,
        status: "0",
        remark: undefined
      }
    },
    //批量删除
    handleDelete(row) {
      let ids = row.dictId || this.dictIds
      this.$confirm("确认删除选中的项吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http.delete("/dictType/deleteByIds/" + ids).then(resp => {
          if (resp.data.code === 200) {
            this.$message.success("删除成功");
            this.formdata();
          }
        }).catch(error => {
          this.$message.warning("删除失败");
        })
      }).catch(error => {
        this.$message.warning("取消删除");
      })
    },
    //选择器
    handleSelectionChange(selection) {
      this.dictIds = selection.map(item => item.dictId)
      this.single = selection.length > 1
      this.multiple = !selection.length
    },
    //页码改变
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.formdata()
    },
    //页码改变
    handleCurrentChange(val) {
      this.queryParams.pageNo = val
      this.formdata()
    },
    //状态格式化
    statusFormatter(row) {
      let dictLabel = "未知"
      this.statusOptions.filter(item => {
        if (item.dictValue === row.status) {
          dictLabel = item.dictLabel
        }
      })
      return dictLabel
    },
    //修改
    handleUpdate(row) {
      this.title = "修改数据"
      this.open = true
      this.form = row
    },
    //添加或修改提交
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$http.post("/dictType/insertAndUpdate", this.form).then(resp => {
            if (resp.data.code === 200) {
              this.$message.success("操作成功")
              this.formdata()
              this.open = false
            } else {
              this.$message.error("操作失败")
            }
          })
        }
      })
    },
    //添加修改取消方法
    cancel() {
      this.open = false
      this.formdata()
      this.form = {
        dictName: "",
        dictType: "",
        status: "",
        remark: ""
      }
    },
  }
}
</script>
<style scoped>

</style>